package com.china.springServer.service.impl;

import com.china.springServer.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.Transaction;

/**
 * @fileName: RedisServiceImpl
 * @author: LmR
 * @createDate: 2023/10/21 14:26
 * @Desc:
 */
@Service
public class RedisServiceImpl implements RedisService {

    @Autowired
    private JedisPool jedisPool;

    @Override
    public void transactionCode() {
        Jedis jedis = jedisPool.getResource();
        Transaction transaction = null;
        try {
            // 开启事务
            transaction = jedis.multi();

            // 执行事务操作
            transaction.set("key1", "value1");
            transaction.set("key2", "value2");
            transaction.set("key3", "value3");

            // 提交事务，执行命令
            transaction.exec();
        } catch (Exception e) {
            transaction.discard();
        } finally {
            jedis.close();
        }

    }
}
